package com.zygh.lz.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    /**
     * 获取某月的最后一天
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        return sdf.format(cal.getTime());
    }


    /**
     * @return 返回开始时间和结束时间之间的每一日
     * @throws ParseException
     */
    public static GregorianCalendar[] getDate(String startTime, String endTime) throws ParseException {
        Vector<GregorianCalendar> v = new Vector<GregorianCalendar>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar gc1 = new GregorianCalendar(), gc2 = new GregorianCalendar();
        gc1.setTime(sdf.parse(startTime));
        gc2.setTime(sdf.parse(endTime));
        do {
            GregorianCalendar gc3 = (GregorianCalendar) gc1.clone();
            v.add(gc3);
            gc1.add(Calendar.DAY_OF_MONTH, 1);
        } while (!gc1.after(gc2));
        return v.toArray(new GregorianCalendar[v.size()]);
    }


    /**
     * 获取时间段内的 年-月-日  日期
     */
    public static List getStartEnd(String startTime, String endTime) throws ParseException {
        GregorianCalendar[] ga = getDate(startTime, endTime);
        List list = new ArrayList();
        for (GregorianCalendar e : ga) {
            String months;
            String days;
            int month = e.get(Calendar.MONTH) + 1;
            if (month < 10) {
                months = "0" + month;
            } else {
                months = String.valueOf(month);
            }
            int day = e.get(Calendar.DAY_OF_MONTH);
            if (day < 10) {
                days = "0" + day;
            } else {
                days = String.valueOf(day);
            }
            String time = e.get(Calendar.YEAR) + "-"
                    + (months) + "-" +
                    days;
            list.add(time);
        }
        return list;
    }


    private static SimpleDateFormat sdf() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }


    // 获得本周一0点时间
    public static String getTimesWeekmorning() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 1);
        return format.format(c.getTime());
    }

    // 获得本周日24点时间
    public static String getTimesWeeknight() {
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 7);
        return formatDate.format(c.getTime());
    }

    //根据当前日期获得最近n周的日期区间（不包含本周）
    public static String getFromToDate(SimpleDateFormat sdf, Date date, int n, int option, int k) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int offset = 0 == option ? 1 - dayOfWeek : 7 - dayOfWeek;
        int amount = 0 == option ? offset - (n - 1 + k) * 7 : offset - k * 7;
        calendar.add(Calendar.DATE, amount);
        return sdf.format(calendar.getTime());
    }

    // 获取上周的开始时间
    public static String getBeginDayOfLastWeek() {
        //上周日期
        SimpleDateFormat sdf = sdf();
        String beginDate = getFromToDate(sdf, new Date(), 1, 0, 1);

        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(beginDate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return sdf.format(calendar.getTime());
    }

    // 获取上周的结束时间
    public static String getEndDayOfLastWeek() {
        //上周日期
        SimpleDateFormat sdf = sdf();
        String endDate = getFromToDate(sdf, new Date(), 1, 1, 1);

        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(endDate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return sdf.format(calendar.getTime());
    }

    /**
     * 判断时间是否在某个时间段内
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        if (nowTime.getTime() == beginTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 将时间戳转换成日期
     * @param timestamp 时间戳
     * @param pattern 转换的日期格式
     * @return
     */
    public static String timestampToDateStr(Long timestamp,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String sd = sdf.format(new Date(timestamp)); // 时间戳转换日期
        return sd;
    }


}
